package icu.luoxi.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import org.apache.ibatis.logging.stdout.StdOutImpl;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;

import javax.sql.DataSource;

/**
 * @author Cao Jiwen
 * @date 2025/4/3 14:23
 * @description 多数据源配置类
 */
public class DataSourceConfig {

    @Configuration
    @MapperScan(basePackages = "icu.luoxi.mapper.master", sqlSessionFactoryRef = "sqlSessionFactoryMaster")
    public static class DataSourceMasterConfig {

        @Primary
        @Bean("dataSourceMaster")
        @ConfigurationProperties(prefix = "spring.datasource.master")
        public DataSource getDataSourceMaster(){
            return DataSourceBuilder.create().build();
        }

        @Primary
        @Bean("sqlSessionFactoryMaster")
        public SqlSessionFactory sqlSessionFactoryMaster(@Qualifier("dataSourceMaster") DataSource dataSource) throws Exception {
            MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
            MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
            bean.setDataSource(dataSource);

            // 配置MyBatis-Plus插件
            mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
            bean.setPlugins(mybatisPlusInterceptor);

            // 设置mapper.xml位置
            bean.setMapperLocations(new PathMatchingResourcePatternResolver()
                    .getResources("classpath*:mapper/master/*.xml"));

            // 设置MyBatis配置
            MybatisConfiguration configuration = new MybatisConfiguration();
            configuration.setLogImpl(StdOutImpl.class); // 设置日志实现
            bean.setConfiguration(configuration);

            return bean.getObject();
        }

        @Primary
        @Bean("sqlSessionTemplateMaster")
        public SqlSessionTemplate sqlSessionTemplateMaster(@Qualifier("sqlSessionFactoryMaster") SqlSessionFactory sqlSessionFactory){
            return new SqlSessionTemplate(sqlSessionFactory);
        }

        @Primary
        @Bean("transactionManagerMaster")
        public PlatformTransactionManager transactionManagerMaster(@Qualifier("dataSourceMaster") DataSource dataSource) {
            return new DataSourceTransactionManager(dataSource);
        }
    }

    @Configuration
    @MapperScan(basePackages = "icu.luoxi.mapper.second", sqlSessionFactoryRef = "sqlSessionFactorySecond")
    public static class DataSourceSecondConfig {

        @Bean("dataSourceSecond")
        @ConfigurationProperties(prefix = "spring.datasource.second")
        public DataSource getDataSourceSecond(){
            return DataSourceBuilder.create().build();
        }

        @Bean("sqlSessionFactorySecond")
        public SqlSessionFactory sqlSessionFactorySecond(@Qualifier("dataSourceSecond") DataSource dataSource) throws Exception {
            MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
            MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
            bean.setDataSource(dataSource);

            // 配置MyBatis-Plus插件
            mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.ORACLE));
            bean.setPlugins(mybatisPlusInterceptor);

            // 设置mapper.xml位置
            bean.setMapperLocations(new PathMatchingResourcePatternResolver()
                    .getResources("classpath*:mapper/second/*.xml"));

            // 设置MyBatis配置
            MybatisConfiguration configuration = new MybatisConfiguration();
            configuration.setLogImpl(StdOutImpl.class); // 设置日志实现
            bean.setConfiguration(configuration);

            return bean.getObject();
        }

        @Bean("sqlSessionTemplateSecond")
        public SqlSessionTemplate sqlSessionTemplateSecond(@Qualifier("sqlSessionFactorySecond") SqlSessionFactory sqlSessionFactory){
            return new SqlSessionTemplate(sqlSessionFactory);
        }

        @Bean("transactionManagerSecond")
        public PlatformTransactionManager transactionManagerSecond(@Qualifier("dataSourceSecond") DataSource dataSource) {
            return new DataSourceTransactionManager(dataSource);
        }
    }
}